import { ContactService } from './contact.service';

// import { InjectModel } from '@nestjs/mongoose';
// import { Model } from 'mongoose';
import { Contact, ContactResponse } from './contact.interface';
import { CreateContactDTO, EditContactDTO } from './contact.dto';
import {
    Body,
    Controller,
    Delete,
    Get,
    Param,
    Post
} from '@nestjs/common'

@Controller('contact')
export class ContactController {
    constructor(private readonly contactService: ContactService) {}

    @Get('all')
    async findAll() : Promise<ContactResponse<Contact[]>> {
        return {
            code: 200,
            data: await this.contactService.findAll(),
            msg: 'success'
        }
    }

    @Get(':_id')
    async find(@Param('_id') _id: string) : Promise<ContactResponse<Contact>> {
        return {
            code: 200,
            data: await this.contactService.find(_id),
            msg: 'success'
        }
    }

    @Post('getby')
    async findWhere(@Body() filter?: object) : Promise<ContactResponse<Contact[]>> {
        return {
            code: 200,
            data: await this.contactService.findWhere(filter),
            msg: 'success'
        }
    }

    @Post('add')
    async addone(@Body() body: CreateContactDTO) : Promise<ContactResponse> {
        await this.contactService.addone(body);
        return {
            code: 200,
            msg: 'success'
        }
    }

    @Post('edit')
    async editone(@Body() body: EditContactDTO) : Promise<ContactResponse> {
        await this.contactService.editone(body._id, body);
        return {
            code: 200,
            msg: 'success'
        }
    }

    @Delete(':_id')
    async deleteone(@Param('_id') _id: string) : Promise<ContactResponse> {
        return {
            code: 200,
            data: await this.contactService.delete(_id),
            msg: 'success'
        }
    }
}
